<%@ page language="java" %>
<%
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("max-age", 0);
    response.setDateHeader("Expires", 0);
%>
<%@ page import="com.scalar.chiptrack.utils.ConfigManager,
                 com.scalar.chiptrack.users.User,
                 com.scalar.chiptrack.commons.SessionKeys"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>

<html>

<HEAD>
	<TITLE>ScalarSoft - ChipTrac - BOM Inventory </TITLE>
    <META http-equiv='Content-Type' content='text/html; charset=windows-1252'>
    <LINK href='<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css' type='text/css' rel='stylesheet'>
	<script src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>
    <script>
    var noOfBomParts = 0;
    function submitPage() {
        //var bomQtyToPull = document.assmReleaseForm.bomQtyToPull.value;
        var bomLotSize = document.assmReleaseForm.bomLotSize.value;
        var bomLotSelected = false;
        var bomLotQtyPulled = 0;
        var pa=document.getElementById("errors");
        pa.innerHTML = '';
        var errors = '';
        var form = document.assmReleaseForm;
        calculateRemainingQty(-1);
        if(pa.innerHTML != '') {
            return;
        }
        for ( var bomPartIdx=0; bomPartIdx < noOfBomParts; bomPartIdx++ ) {
            var bomQtyPulled = form['bomPart['+bomPartIdx+'].qtyPulled'].value;
            var qtyToBePulled = form['bomPart['+bomPartIdx+'].qtyToBePulled'].value;
            var bomPartNumber = form['bomPart['+bomPartIdx+'].bomPartNumber'].value;
            if ( +bomQtyPulled != +qtyToBePulled ) {
                errors = errors +  "<li>For <bean:message key="label.operation.release.assm.BOMLot" /> part: <font color=blue>" +bomPartNumber+"</font> pulled qty must be equal to qty to be pulled("+qtyToBePulled+")" ;
            }
        }
        if( errors != '' ) {
            pa.innerHTML = errors;
            return;
        }
        for( var i=0; i< bomLotSize;i++ ) {
            document.assmReleaseForm['bomLotInventoryList['+ i +'].qtyPulled'].disabled = false;
        }
        document.assmReleaseForm['totalBOMDieQtyPulled'].value = true;
        document.assmReleaseForm.action = "<%=chipTracContext%>/assmRelease.do?action=closeAssemblyBomInventoryLotInfo";
        document.assmReleaseForm.submit();
    }



    function moveToQtyPulled( index ) {
        if( document.assmReleaseForm[ 'bomLotInventoryList[' + index + '].selected' ].checked == true ) {
            document.assmReleaseForm['bomLotInventoryList['+ index +'].qtyPulled'].disabled = false;
            //document.assmReleaseForm['bomLotInventoryList['+ index +'].qtyPulled'].value = document.assmReleaseForm['bomLotInventoryList['+ index +'].qtyAvailable'].value
            document.assmReleaseForm['bomLotInventoryList['+ index +'].qtyPulled'].value = 0;
            document.assmReleaseForm['bomLotInventoryList['+ index +'].qtyPulled'].select();
        } else {
            document.assmReleaseForm['bomLotInventoryList['+ index +'].qtyPulled'].disabled = true;
            document.assmReleaseForm['bomLotInventoryList['+ index +'].qtyPulled'].value = 0;
        }
        calculateRemainingQty( index );
    }


    function calculateRemainingQty( index )
    {
        var form                        = document.assmReleaseForm;
        var bomLotSize                  = form.bomLotSize.value;

        var pa = document.getElementById("errors");
        pa.innerHTML = '';
        var errors = '';
        for ( var bomPartIdx=0; bomPartIdx < noOfBomParts; bomPartIdx++ ) {
            var fromIdx = form['bomPart['+bomPartIdx+'].lotFromIdx'].value;
            var toIdx = form['bomPart['+bomPartIdx+'].lotToIdx'] != null ? form['bomPart['+bomPartIdx+'].lotToIdx'].value : bomLotSize-1;
            var bomLotQtyPulled = 0;
            if( index < 0 || (index >= +fromIdx && index <= +toIdx) ) {
                for( var i = fromIdx; i <= toIdx; i++ ) { //for each bom Lot
                    if( form['bomLotInventoryList[' + i + '].selected'].checked ) {
                        var qtyPulledObj = form['bomLotInventoryList[' + i + '].qtyPulled'];
                        var qtyPulled    = qtyPulledObj.value;
                        if( qtyPulled < 0 ) {
                            errors = errors +  "<li> Please Enter the Valid Qty Pulled in <bean:message key="label.operation.release.assm.BOMLot"/> Lot Information";
                        } else if( ( +qtyPulled >  eval( form['bomLotInventoryList['+ i +'].qtyAvailable'].value ) ) && ( form['bomLotInventoryList['+ i +'].lotNumber'].value != '') ) {
                            errors = errors +  "<li> The Quantity Pulled can not be Greater than Quantity Available for <bean:message key="label.operation.release.assm.BOMLot" /> Lot <font color=blue>"+ form['bomLotInventoryList['+ i +'].lotNumber'].value + "</font>" ;
                        } else if( ( (+qtyPulled) + (+bomLotQtyPulled) ) >  eval( form['bomPart['+bomPartIdx+'].qtyToBePulled'].value )  ) {
                            errors = errors +  "<li> The Total Qty Pulled can not be Greater than Qty to be pulled for <bean:message key="label.operation.release.assm.BOMLot" /> Lot <font color=blue>"+ form['bomLotInventoryList['+ i +'].lotNumber'].value + "</font>" ;
                            qtyPulledObj.focus();
                            bomLotQtyPulled  += parseInt( 0 );
                        } else {
                            bomLotQtyPulled = (+bomLotQtyPulled) + (+qtyPulled);
                        }
                        if( errors != '' ){
                            break;
                        }
                    }
                }
                form['bomPart['+bomPartIdx+'].qtyPulled'].value = bomLotQtyPulled;
                var qtyToBePulled = form['bomPart['+bomPartIdx+'].qtyToBePulled'].value;
                form['bomPart['+bomPartIdx+'].remainingQtyToBePulled'].value =  (+qtyToBePulled) - (+bomLotQtyPulled);
            }
        }
        if( errors != '' ) {
            pa.innerHTML = errors;
            return;
        }
    }

    function onScreenLoad()
    {
        <%--document.assmReleaseForm.bomQtyToPull.value = Math.floor( ((<bean:write name="assmReleaseForm" property="totalDieQtyPulled"/>) / (<bean:write name="assmReleaseForm" property="diesPerPackage"/> ) ) ) * <bean:write name="assmReleaseForm" property="bomQtyPerDie"/>;--%>

        //document.assmReleaseForm.remainingDieQtyToPull.value = eval(document.assmReleaseForm.bomQtyToPull.value - document.assmReleaseForm.totalBOMDieQtyPulled.value);
        var form = document.assmReleaseForm;
        var bomLotSize = form.bomLotSize.value;
        for ( var bomPartIdx=0; bomPartIdx < noOfBomParts; bomPartIdx++ ) {
            var fromIdx = form['bomPart['+bomPartIdx+'].lotFromIdx'].value;
            var toIdx = form['bomPart['+bomPartIdx+'].lotToIdx'] != null ? form['bomPart['+bomPartIdx+'].lotToIdx'].value : bomLotSize-1;
            var bomLotQtyPulled = 0;
            for( var i = fromIdx; i <= toIdx; i++ ) { //for each bom part
                if( document.assmReleaseForm[ 'bomLotInventoryList[' + i + '].qtyPulled' ].value > 0 ) {
                    document.assmReleaseForm['bomLotInventoryList['+ i +'].selected'].checked = true;
                    document.assmReleaseForm['bomLotInventoryList['+ i +'].qtyPulled'].disabled = false;
                }
                if(document.assmReleaseForm.status.value == 'Closed' || document.assmReleaseForm.status.value == 'CLOSED') {
                    document.assmReleaseForm['bomLotInventoryList['+ i +'].selected'].disabled = true;
                }

                var qtyPulled = form['bomLotInventoryList[' + i + '].qtyPulled'].value;
                bomLotQtyPulled = (+bomLotQtyPulled) + (+qtyPulled);
            }
            form['bomPart['+bomPartIdx+'].qtyPulled'].value = bomLotQtyPulled;
            var totalBuildQty = form['totalBuildQty'].value;
            var bomQtyPerDie = form['bomPart['+bomPartIdx+'].qtyPerDie'].value;
            var bomQtyToBePulled = (+totalBuildQty) * (+bomQtyPerDie);
            form['bomPart['+bomPartIdx+'].qtyToBePulled'].value = bomQtyToBePulled;
            form['bomPart['+bomPartIdx+'].remainingQtyToBePulled'].value = (+bomQtyToBePulled) - (+bomLotQtyPulled);

        }
        /*var bomLotSize = document.assmReleaseForm.bomLotSize.value;
        for( var i=0; i< bomLotSize;i++ ) {
            if( document.assmReleaseForm[ 'bomLotInventoryList[' + i + '].qtyPulled' ].value > 0 ) {
                document.assmReleaseForm['bomLotInventoryList['+ i +'].selected'].checked = true;
                document.assmReleaseForm['bomLotInventoryList['+ i +'].qtyPulled'].disabled = false;
            }
            if(document.assmReleaseForm.status.value == 'Closed' || document.assmReleaseForm.status.value == 'CLOSED') {
                document.assmReleaseForm['bomLotInventoryList['+ i +'].selected'].disabled = true;
            }
        }*/
        if(document.assmReleaseForm.status.value == 'Closed' || document.assmReleaseForm.status.value == 'CLOSED') {
            document.assmReleaseForm.waferMapOrInkedForBom.disabled = true;
        }
        //calculateRemainingQty(-1);
    }
    </script>
</HEAD>

<BODY bgcolor='ffffff' marginheight='0' marginwidth='0' topmargin='0' leftmargin='0' onload="javascript:onScreenLoad()">

<html:form name="assmReleaseForm"  type="com.scalar.chiptrack.operations.release.forms.assmReleaseForm" action="/assmRelease.do">

<table align='center' border='0' width='100%' >
    <tr>
        <td colspan='4' align='center' class="NArialL"><bean:message key="label.operation.release.assm.BOMLot" /> Lot Information</td>
    </tr>
    <tr>
        <td colspan='4' align='center'>&nbsp;</td>

        <html:hidden name="assmReleaseForm" property="totalBOMDieQtyPulled"/>
        <html:hidden name="assmReleaseForm" property="status"/>
    </tr>

    <tr><td colspan='4' align="left" class='NArialL'>&nbsp;<FONT id="errors" color='FF0000'></font></td></tr>
    <tr><td colspan='4' class="NArialL"><chiptrack:errors styleClass="NArialL"/></td></tr>
    <tr><td colspan='4' class="NArialL"><chiptrack:info styleClass="NArialL"/></td></tr>    
    <!-- BOM Lot Inventory Information -->
    
    <tr>
        <td class='NArialL' width="50%">Wafer Map/Inked:</td>
        <td class='NArialL'>
            <html:select name="assmReleaseForm" property="waferMapOrInkedForBom" styleClass="txfield">
                <html:options property="waferMapOrInkedList"/>
            </html:select>
        </td>
        <td colspan='2'>&nbsp;</td>
    </tr>
    <tr>
        <td class='NArialL' width="50%">Dies/Package:</td>
        <td class='NArialL'> <bean:write name="assmReleaseForm" property="diesPerPackage"/>            
        </td>
        <td colspan='2'>&nbsp;</td>
    </tr>
    <tr>
        <td class='NArialL' width="50%">Total Assembly Die Quantity pulled :</td>
        <td class='NArialL'>
            <html:hidden name="assmReleaseForm" property="totalDieQtyPulled"/>
            <input type="text" name="totalDieQtyPulledTxt" class="txfield" disabled="true" value="<nested:write property="totalDieQtyPulled"/>"/>            
        </td>
        <td colspan='2'>&nbsp;</td>
    </tr>
    <tr>
        <td class='NArialL' width="50%">Total Assembly Die Build Quantity :</td>
        <td class='NArialL'>
            <input type="text" name="totalBuildQty" class="txfield" disabled="true" value="<%=request.getParameter("totalBuildQty")%>"/>
        </td>
        <td colspan='2'>&nbsp;</td>
    </tr>
    <tr><td colspan='4'><IMG src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='10'></td></tr>
    <tr>
        <td colspan='4'>
            <table border="0" width="55%">
                <nested:size id='bomLotSize' name='assmReleaseForm' property='bomLotInventoryList'/>
                <input type='hidden' name='bomLotSize' value='<%=bomLotSize%>'/>
                <%
                    String prevBomPart = "";
                    int noOfBomParts = 0;
                %>
					<nested:iterate name="assmReleaseForm" property="bomLotInventoryList" id="bomLot" type="com.scalar.chiptrack.operations.release.ReleaseLot" indexId="index" >
						<logic:notEqual value="<%=prevBomPart%>" property="configuredBomPartNumber" name="bomLot">
							<logic:notEqual value="0" name="index">
								<input type="hidden" name="bomPart[<%=noOfBomParts-1%>].lotToIdx" value="<%=index.intValue()-1%>"/>
		                    </logic:notEqual>
							<%
								prevBomPart = bomLot.getConfiguredBomPartNumber();
								noOfBomParts++;
							%>
							<logic:notEqual value="0" name="index">
								</table>
									</td>
										</tr>
							</logic:notEqual>
		                    <tr><td colspan="4"><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='10'></td></tr>
							<tr class="NArialL">
								<td align="center" bgcolor="#993333" class="NArialL" rowspan="2"><%--<img src="<%=chipTracContext%>/jsp/includes/images/plus.gif">--%>&nbsp;</td>
								<td align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF"><bean:message key="label.operation.release.assm.BOMLot" /> Part Number</font></td>
								<td align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF"><bean:message key="label.operation.release.assm.BOMLot" /> Qty per Die</font></td>
								<td align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF"><bean:message key="label.operation.release.assm.BOMLot" /> Qty Pulled</font></td>
								<td align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF"><bean:message key="label.operation.release.assm.BOMLot" /> Qty to be Pulled</font></td>
								<td align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF">Remaining Qty to be Pulled</font></td>
							</tr>
		                    <tr class="NArialL">
				                <input type="hidden" name="bomPart[<%=noOfBomParts-1%>].lotFromIdx" value="<%=index.intValue()%>"/>
						        <input type="hidden" name="bomPart[<%=noOfBomParts-1%>].bomPartNumber" value="<nested:write property="configuredBomPartNumber"/>"/>

								<td align="center" bgcolor="#D8D8D8" class="NArialL"><nested:write property="configuredBomPartNumber"/></td>
		                        <td align="center" bgcolor="#D8D8D8" class="NArialL"><input type="text" class="valuetxfield" disabled='true' size=10 name="bomPart[<%=noOfBomParts-1%>].qtyPerDie" value="<%=bomLot.getBomQtyPerDie()%>"/></td>
				                <td align="center" bgcolor="#D8D8D8" class="NArialL"><input type="text" class="valuetxfield" disabled='true' size=10 name="bomPart[<%=noOfBomParts-1%>].qtyPulled" value=""/></td>
						        <td align="center" bgcolor="#D8D8D8" class="NArialL"><input type="text" class="valuetxfield" disabled='true' size=10 name="bomPart[<%=noOfBomParts-1%>].qtyToBePulled" value=""/></td>
								<td align="center" bgcolor="#D8D8D8" class="NArialL"><input type="text" class="valuetxfield" disabled='true' size=10 name="bomPart[<%=noOfBomParts-1%>].remainingQtyToBePulled" value=""/></td>
							</tr>
                    <tr>
                        <td colspan="6" width="100%">
                            <table width="100%">
                                <tr>
                                    <td width="5%" align="center" bgcolor="#A4A4A4" class="NArialL"><font color="#000000">&nbsp;</font></td>
                                    <%
                                        if ( ConfigManager.isBomPullingByWaferId() )
                                        {
                                    %>
                                        <td width="10%" align="center" bgcolor="#A4A4A4" class="NArialL"><font color="#000000">Wafer Id</font></td>
                                    <% } %>
	                                <td width="20%" align="center" bgcolor="#A4A4A4" class="NArialL"><font color="#000000">Part Number</font></td>
                                    <td width="20%" align="center" bgcolor="#A4A4A4" class="NArialL"><font color="#000000">Lot Number</font></td>
                                    <td width="20%" align="center" bgcolor="#A4A4A4" class="NArialL"><font color="#000000">Qty Available</font></td>
                                    <td width="20%" align="center" bgcolor="#A4A4A4" class="NArialL"><font color="#000000">Qty Pulled</font></td>
                                    <td width="20%" align="center" bgcolor="#A4A4A4" class="NArialL"><font color="#000000">Vendor</font></td>
                                </tr>
                </logic:notEqual>
                                <tr  bgcolor='<%=index.intValue() % 2 == 0 ? "#FFFFEE" : "#DEE1E2"%>'>
                                    <nested:hidden property="lotId"/>
                                    <td>
                                        <%  String tempStr = "moveToQtyPulled(" + index + ")"; %>
                                        <nested:checkbox property="selected" styleClass="txfield" onclick="<%= tempStr %>"/>
                                        <nested:hidden property="newLot"/>
                                    </td>
                                    <%
                                        if ( ConfigManager.isBomPullingByWaferId() )
                                        {
                                    %>
                                        <td><nested:text property="waferId" styleClass="txfield" size = "5" disabled='true'/></td>
                                    <% } %>
                                    <td>
                                        <nested:text property="bomPartNumber" styleClass="txfield" disabled='true'/>
                                    </td>
                                    <td>
                                        <nested:text property="lotNumber" styleClass="txfield" disabled='true'/>
                                    </td>
                                    <td>
                                        <nested:text property="qtyAvailable" size ="10" styleClass="valuetxfield" disabled='true'/>
                                    </td>
                                    <%  tempStr = "calculateRemainingQty(" + index + ")"; %>
                                    <td>
                                        <nested:text property="qtyPulled" styleClass="valuetxfield"  size ="10" disabled="true" onkeypress="allowInteger()" onkeyup="<%= tempStr %>"/>
                                    </td>
                                    <td class="NArialL">
                                        <nested:write property="currentVendor"/>
                                    </td>
                                </tr>
                </nested:iterate>
                <script type="text/javascript">
                    noOfBomParts = <%=noOfBomParts%>;
                </script>
            </table>
        </td>
    </tr>
    <!-- BOM Lot Inventory Informatin -->
    <tr><td colspan='4'><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='10'></td></tr>
    <tr>
        <td colspan="6" align="center">
        <a href="javascript:submitPage();"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/save.gif" width="53" height="17"  onmouseover="window.status='Click to Save Assembly Release BOM Lot Information';return true" onmouseout="window.status='';return true" title="Click here to Save Assembly Release"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <a href="javascript:close();"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" width="53" height="17" onmouseover="window.status='Click to Reset';return true" onmouseout="window.status='';return true" title="Click here to close the window"></a>
        </td>
    </tr>
</table>
</html:form>
</body>
</html>